package com.changgou.page.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.entity.Result;
import com.changgou.goods.feign.CategoryFeign;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Category;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.page.service.PageService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: HuangFu
 * @Date: 2020/2/5 15:51
 * @Description: 页面静态化实现类
 */
@Service
public class PageServiceImpl implements PageService {

    @Autowired
    private CategoryFeign categoryFeign;
    @Autowired
    private SpuFeign spuFeign;
    @Autowired
    private SkuFeign skuFeign;
    /**
     * 生成地址
     */
    @Value("${system.thymelef.pagepath}")
    private String pagepath;

    /**
     * Thymeleaf模板引擎
     */
    @Autowired
    private TemplateEngine templateEngine;

    /***
     * 生成静态化页面
     * @param spuId
     * */
    @Override
    public void generateItemPage(String spuId) {
        //获取context对象,用于存放商品详情数据
        Context context = new Context();
        Map<String, Object> itemData = this.findItemData(spuId);
        if(itemData==null){
            return;
        }
        //设置数据
        context.setVariables(itemData);

        //获取商品详情页生成的指定位置
        File dir = new File(pagepath);
        if(!dir.exists()){
            dir.mkdirs();
        }
        //定义生成文件
        File file = new File(pagepath + "/" + spuId + ".html");
        Writer writer = null;
        try {
            writer = new PrintWriter(file);
            /***
             * 1.模板名称
             * 2.context对象,包含了模板需要的数据
             * 3.输出流,指定文件输出位置
             */
            templateEngine.process("item",context,writer);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(writer!=null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }




    /**
     * 获取页面需要的数据
     * @param spuId
     * @return
     */
    private Map<String, Object> findItemData(String spuId) {
        Map<String, Object> resultMap = new HashMap<>(16);

        //获取Spu数据
        Result<Spu> spuResult = spuFeign.findSpuById(spuId);
        Spu spu = spuResult.getData();
        //查询成功，且数据不为空
        if(spuResult.isFlag()&&spu!=null){
            resultMap.put("spu",spu);
            //设置图片信息
            if(StringUtils.isNotEmpty(spu.getImages())){
                resultMap.put("imageList",spu.getImages().split(","));
            }
            //设置商品的分类
            Result<Category> category1Result = categoryFeign.findById(spu.getCategory1Id());
            resultMap.put("category1",category1Result.getData());
            Result<Category> category2Result = categoryFeign.findById(spu.getCategory2Id());
            resultMap.put("category2",category2Result.getData());
            Result<Category> category3Result = categoryFeign.findById(spu.getCategory3Id());
            resultMap.put("category3",category3Result.getData());

            //获取Sku集合数据并设置
            List<Sku> skuList = skuFeign.findListBySpuId(spuId);
            resultMap.put("skuList",skuList);

            //设置规格定义信息
            resultMap.put("specificationList", JSON.parseObject(spu.getSpecItems(), Map.class));
            return resultMap;
        }
        return null;
    }

}
